<template>
  <div class="classification">
    <el-button
      type="primary"
      icon="el-icon-plus"
      @click="elDialogShow(true)"
      style="margin-bottom: 20px"
      >添加
    </el-button>
    <el-table :data="tableData" style="width: 100%;" height="450">
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="name" label="分类名"> </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column prop="updateTime" label="修改时间"> </el-table-column>
      <el-table-column label="操作" width="120px">
        <template slot-scope="scope">
          <el-button
            type="primary "
            class="button"
            @click="elDialogShow(false, scope.row)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            type="danger"
            class="button"
            @click="deleteDialogVisibleShow(scope.row.id)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      page-size="5"
      style="text-align: center;margin-top: 20px;"
      @current-change="currentChange"
    >
    </el-pagination>
    <el-dialog
      :title="isAdd ? '添加分类' : '修改分类'"
      :visible.sync="dialogVisible"
      width="30%"
      center
    >
      <span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="ID" v-if="!isAdd">
            <el-input v-model="form.id" disabled></el-input>
          </el-form-item>
          <el-form-item label="分类名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="分类排序">
            <el-input-number :min="1" v-model="form.sort"></el-input-number>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="change()">{{
          isAdd ? "添 加" : "修 改"
        }}</el-button>
      </span>
    </el-dialog>
    <el-dialog title="提示" :visible.sync="deleteDialogVisible" width="30%">
      <span>删除该分类,会将该分类下的所有产品删除,确定要删除吗?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteGoods()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "CatcoffeeClassification",

  data() {
    return {
      tableData: [],
      selectInput: "",
      isAdd: true,
      changeid: "",
      dialogVisible: false,
      form: {},
      deleteIndex: 0,
      deleteDialogVisible: false,
      page:1
    };
  },

  mounted() {
    this.getClass();
  },

  methods: {
    getClass() {
      let select = this.selectInput != "" ? "&name=" + this.selectInput : "";
      this.$store.commit("setIsLoad", true);
      this.$axios
        .get("/class/select?page="+this.page+"&pageSize=5" + select)
        .then((res) => {
          this.tableData = res.data.data.records;
          this.total = res.data.data.total;
          this.$store.commit("setIsLoad", false);
        });
    },
    change() {
      if (this.isAdd) {
        this.$store.commit("setIsLoad", true);
        this.$axios
          .post("/class/add", {
            name: this.form.name,
            sort: this.form.sort,
          })
          .then((res) => {
            if (res.data.code == 200) {
              this.$notify.success({
                title: "成功",
                message: "添加成功",
              });
              this.dialogVisible = false;
            } else {
              this.$notify.error({
                title: "错误",
                message: res.data.msg,
              });
            }
            this.$store.commit("setIsLoad", false);
            this.getClass();
          });
      } else {
        this.$store.commit("setIsLoad", true);
        this.$axios
          .post("/class/update", {
            name: this.form.name,
            sort: this.form.sort,
          })
          .then((res) => {
            if (res.data.code == 200) {
              this.$notify.success({
                title: "成功",
                message: "修改成功",
              });
              this.dialogVisible = false;
            } else {
              this.$notify.error({
                title: "错误",
                message: res.data.msg,
              });
            }
            this.$store.commit("setIsLoad", false);
            this.getClass();
          });
      }
    },
    elDialogShow(isAdd, row = {}) {
      this.form = row;
      this.isAdd = isAdd;
      this.dialogVisible = true;
    },
    deleteDialogVisibleShow(id) {
      this.deleteDialogVisible = true;
      this.deleteIndex = id;
    },
    deleteGoods() {
      this.$store.commit("setIsLoad", true);
      this.$axios.get("/class/delete?id=" + this.deleteIndex).then((res) => {
        if (res.data.code == 200) {
          this.$notify.success({
            title: "成功",
            message: "删除成功",
          });
        } else {
          this.$notify.success({
            title: "失败",
            message: res.data.msg,
          });
        }
        this.deleteDialogVisible = false;
        this.$store.commit("setIsLoad", true);
        this.getClass();
      });
    },
    currentChange(page){
      this.page = page;
      this.getClass();
    }
  },
};
</script>

<style scoped>
.classification {
  padding: 20px;
}
</style>